import generateRandomData from "./generateData.js";
import {kmeans, encodePerson} from "./cluster.js";
import {visualize} from "./visualize.js";
import {PCA} from 'ml-pca';

document.getElementById("run").onclick = () => {
    let k = 4;
    // 生成随机的数据
    const people = generateRandomData(200);
    console.log("people:", people);
    // 向量化
    const vectors = people.map(encodePerson);
    console.log("vectors:", vectors);
    // PCA降维
    const pca = new PCA(vectors);
    const projected = pca.predict(vectors, {nComponents: 2}).to2DArray();
    // K-means进行聚类
    const {assignments} = kmeans(vectors, k);
    console.log("assignments:", assignments);
    // 可视化
    visualize(people, assignments, k, projected);
};
